﻿--[[
   @brief    contact item node
   @author   perpu
   @version  1.0
   @date     2016-06-15
--]]

chat_vlist_node = {
    update = function(nodetype, control_id, info)
        if nodetype == friend_vlist_model.NODE_TYPE_DISCUSSION_NODE or
           nodetype == friend_vlist_model.NODE_TYPE_GROUP_NODE then
            local group_info = info["group_info"];
            if group_info == nil then
                return;
            end;
            friend_vlist_model.virtual_node_id_to_group_info[control_id] = group_info;

            local flag_id = ui_utility.get_ctrl_id_by_path(control_id, 'Ctn_Name\\Ctn_Flag');
            if group_info.group_type == "MatchLiveRoom" or
               group_info.group_type == "CommonTopicRoom" then
                ui_utility.setproperty_by_id(flag_id, "Visible", "true");
            else
                ui_utility.setproperty_by_id(flag_id, "Visible", "false");
            end

            if group_info.name == nil then
                group_info.name = "";
            end

            local static_name = group_info.name;
            --[[if chat_vlist_node.is_new_group(group_info.id) then
                static_name = '[NEW]'..group_info.name;
            end]]--

            local static_id = ui_utility.get_ctrl_id_by_path(control_id, 'Ctn_Name\\Static_Nick_Name');
            local ui_str, str_holder = U8toCUIStr(info.name);
            if group_info.group_type == "MatchLiveRoom" or
               group_info.group_type == "CommonTopicRoom" then
                ui_utility.setproperty_by_id(static_id,'Caption', " "..static_name);
            else
                ui_utility.setproperty_by_id(static_id,'Caption', static_name);
            end

            --[[local id = ui_utility.get_ctrl_id_by_path(root_id, "Button_Head_Image");
            ui_utility.setproperty_by_id(id, "Background", string.format("ONEIMAGE:social_network\\friend-head-small.png|%s;Circle;null;", banner_info.face_url));
    
            id = ui_utility.get_ctrl_id_by_path(root_id, "Static_Nick_Name");
            ui_utility.setproperty_by_id(id, "Caption", banner_info.name);]]--
            local banner_info = info['group_info'];
            local root_id = control_id;

            local face_url = "";
            if banner_info.type == "friend" then
                face_url = string.format("ONEIMAGE:client_social\\default_head.png|%s;Circle;null;", group_info.face_url);
            elseif banner_info.group_type == "MobileDiscus" then
                face_url = string.format("ONEIMAGE:client_social\\discus.png;Circle;null;");
            else
                face_url = string.format("ONEIMAGE:client_social\\group.png|%s;Circle;null;", group_info.face_url);
            end
            local head_id = ui_utility.get_ctrl_id_by_path(control_id, 'Button_Head_Image');
            ui_utility.setproperty_by_id(head_id, "Background", face_url);


            local msg_text = '';
            if banner_info.last_msg ~= nil and banner_info.last_msg.msg_text ~= nil then
                msg_text = banner_info.last_msg.msg_text;
            end;
            id = ui_utility.get_ctrl_id_by_path(root_id, "Static_Last_Msg");
            ui_utility.setproperty_by_id(id, "Caption", msg_text);

            local format_time = '';
            if banner_info.last_msg ~= nil and banner_info.last_msg.format_time ~= nil then
                format_time = banner_info.last_msg.format_time;
            end;
            id = ui_utility.get_ctrl_id_by_path(root_id, "Static_Last_Msg_Time");
            ui_utility.setproperty_by_id(id, "Caption", format_time);

            local show_mask_flag = "false";
            if banner_info.type == "group" and  banner_info.mask_flag ~= nil and banner_info.mask_flag > 0 then
                show_mask_flag = "true";
            end
            id = ui_utility.get_ctrl_id_by_path(root_id, "Button_Mask_Image");
            ui_utility.setproperty_by_id(id, "Visible", show_mask_flag); 

            local id_normal = ui_utility.get_ctrl_id_by_path(root_id, "Button_New_Message");
            local id_big = ui_utility.get_ctrl_id_by_path(root_id, "Button_New_Message_Big");
            if banner_info.unread_msg_cnt == nil then
                banner_info.unread_msg_cnt = 0;
            end;
            if banner_info.unread_msg_cnt == 0 then
                ui_utility.setproperty_by_id(id_normal, "Visible", "false");
                ui_utility.setproperty_by_id(id_big, "Visible", "false");
            elseif banner_info.unread_msg_cnt <= 99 then
                ui_utility.setproperty_by_id(id_normal, "Visible", "true");
                ui_utility.setproperty_by_id(id_normal, "Caption", tostring(banner_info.unread_msg_cnt));
                ui_utility.setproperty_by_id(id_big, "Visible", "false");
            elseif banner_info.unread_msg_cnt > 99 then
                ui_utility.setproperty_by_id(id_normal, "Visible", "false");
                ui_utility.setproperty_by_id(id_big, "Visible", "true");
                ui_utility.setproperty_by_id(id_big, "Caption", "99+");
            end

            --选中态
            if chat_vlist_node.is_same_node(chat_vlist_model.current_selected_, group_info) then
                chat_vlist_node.set_selected_state(control_id, true);
            else
                chat_vlist_node.set_selected_state(control_id, false);
            end
        end;
    end;

    is_new_group = function(group_id)
        return true;
    end;

    restore_banner_bk = function()
        for k, v in pairs(friend_vlist_model.virtual_node_id_to_group_info) do
            if friend_vlist_model.current_hover_group_id ~= v.group_id then
                chat_vlist_node.set_normal_state(k);
            end
        end
    end;

    is_same_node = function(node_info_a, node_info_b)
        if node_info_a == nil then
            return false;
        end;
        if node_info_b == nil then
            return false;
        end;
        if node_info_a.id == node_info_b.id and
        node_info_a.type == node_info_b.type then
            return true;
        end;
        
        return false;
    end;

    --以下是鼠标键盘消息处理
    mouse_over_banner = function(uilib, ctrl_id)
        local g_info = friend_vlist_model.virtual_node_id_to_group_info[ctrl_id];
        chat_vlist_node.set_hover_state(ctrl_id);
    end;

    mouse_leave_banner = function(uilib, ctrl_id)
        local g_info = friend_vlist_model.virtual_node_id_to_group_info[ctrl_id];
        chat_vlist_node.set_normal_state(ctrl_id);
    end;

    current_selected_ctrl_id_ = nil;
    on_left_button_down = function(uilib, ctrl_id)
        chat_vlist_node.current_selected_ctrl_id_ = ctrl_id;
        local g_info = friend_vlist_model.virtual_node_id_to_group_info[ctrl_id];
        --local selected = {group_id = 1, friend_id = 2, type = g_info.type};
        local selected = {};
        selected["id"] = g_info.id;
        selected["type"] = g_info.type;
        chat_vlist_model.current_selected_ = selected;
        chat_vlist_node.set_selected_state(ctrl_id, true);
    end;

    on_right_button_down = function(uilib, ctrl_id)
        chat_vlist_node.on_left_button_down(uilib, ctrl_id);
    end;

    on_right_button_up = function(uilib, ctrl_id, x, y)
        local banner_info = friend_vlist_model.virtual_node_id_to_group_info[ctrl_id];
        if banner_info ~= nil then
            chat_vlist_node.send_hide_group_tips_event(1);

            local pControl = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
            local rc = pControl:GetRectEx(TRUE,TRUE);
            if banner_info.type == "group" then
                local param = {};
                param["group_id"] = banner_info.id;
                param["x"] = rc.left + x;
                param["y"] = rc.top + y;
                event_center.send_event(group_event.SHOW_GROUP_MENU,base_utility.table_to_json(param))
            else
                local param = {};
                param["bShow"] = true;    
                param["friend_id"] = banner_info.id;
                param["x"] = rc.left + x;
                param["y"] = rc.top + y;
                event_center.send_event(group_event.SHOW_FRIEND_MENU,base_utility.table_to_json(param));
            end
        end       
    end;

    mouse_db_click = function(uilib, ctrl_id)
        local selected = chat_vlist_node.current_selected_ctrl_id_;
        local banner_info = friend_vlist_model.virtual_node_id_to_group_info[selected];
        if banner_info ~= nil then
            local table_param = {};

            local info = {};
            info.id     = banner_info.id;
            info.is_new = false;
            info.time   = banner_info.time;

            if banner_info.type == "group" then 
                if banner_info.group_type == "MatchLiveRoom" or 
                   banner_info.group_type == "CommonTopicRoom" then
                    comm_center.call_service('i_service_social_open_live_room_talk', {group_id = banner_info.id});
                    comm_center.call_service('i_service_increase_click_live_room_msg_node_cnt', {});
                    return;                 
                else
                    info.group_type = banner_info.group_type;
                    local groups = {};
                    table.insert(groups, info);
                    table_param["groups"] = groups;
                end
            else
                local friends = {};
                table.insert(friends, info);
                table_param["friends"] = friends;
            end
            event_center.send_event(group_event.SHOW_TALK_DIALOG, base_utility.table_to_json(table_param));
        end
    end;

    start_show_group_tip_timer = function()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval('chat_vlist_node.show_group_tip_timer_process', 300);
    end;

    stop_show_group_tip_timer = function()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('chat_vlist_node.show_group_tip_timer_process');
    end;

    show_group_tip_timer_process = function()
        chat_vlist_node.stop_show_group_tip_timer();
        local banner_info = friend_vlist_model.virtual_node_id_to_group_info[chat_vlist_node.current_hover_id_];
        if banner_info ~= nil then
            if banner_info.type == "group" then
                chat_vlist_node.send_show_group_tips_event();
            else
                chat_vlist_node.send_show_friend_tips_event();
            end
        end
    end;

    send_show_group_tips_event = function()
        local banner_info = friend_vlist_model.virtual_node_id_to_group_info[chat_vlist_node.current_hover_id_];
        if banner_info == nil then
            return;
        end

        local pControl = ui_utility.get_ctrl_ptr_by_id(chat_vlist_node.current_hover_id_);
        local rc = pControl:GetRectEx(TRUE,TRUE);
        local param = {};
        param["bShow"] = true;    
        param["group_id"] = banner_info.id;

        local out = comm_center.call_service("i_service_mainframe_get_current_mode", {});
        if out.mode == 0 then
            param["x"] = rc.left + 18;
        else
            param["x"] = rc.left + 16;
        end
        param["y"] = rc.top;
        event_center.send_event(group_event.SHOW_GROUP_TIPS,base_utility.table_to_json(param));
    end;

    send_show_friend_tips_event = function()
        local banner_info = friend_vlist_model.virtual_node_id_to_group_info[chat_vlist_node.current_hover_id_];
        if banner_info == nil then
            return;
        end

        local pControl = ui_utility.get_ctrl_ptr_by_id(chat_vlist_node.current_hover_id_);
        local rc = pControl:GetRectEx(TRUE,TRUE);

        local param = {};
        param["bShow"] = true;    
        param["friend_id"] = banner_info.id;

        local out = comm_center.call_service("i_service_mainframe_get_current_mode", {});
        if out.mode == 0 then
            param["x"] = rc.left + 18;
        else
            param["x"] = rc.left + 16;
        end
        param["y"] = rc.top;
        event_center.send_event(group_event.SHOW_FRIEND_TIPS,base_utility.table_to_json(param));
    end;

    mouse_over_imgage = function(uilib, ctrl_id)
        local p_id = ui_utility.get_ctrl_parent_id(ctrl_id);
        local banner_info = friend_vlist_model.virtual_node_id_to_group_info[p_id];
        chat_vlist_node.set_hover_state(p_id);

        if (group_menu_view == nil or group_menu_view.is_show() == false) and
           (friend_menu_view == nil or friend_menu_view.is_show() == false) then

            local show_tips = true;
            if banner_info.type == "group" then
                if banner_info.group_type == "Public" then
                    chat_vlist_node.send_hide_friend_tips_event(1);
                else
                    show_tips = false;
                end
            else
                --friend tips
                chat_vlist_node.send_hide_group_tips_event(1);
            end
            if show_tips then
                chat_vlist_node.current_hover_id_ = p_id;
                chat_vlist_node.stop_show_group_tip_timer();
                chat_vlist_node.start_show_group_tip_timer();
            end
        end
    end;

    mouse_leave_imgage = function(uilib, ctrl_id)
        base_utility.log("[chat_vlist_node] mouse_leave_group_imgage");

        chat_vlist_node.stop_show_group_tip_timer();
        if (group_menu_view == nil or group_menu_view.is_show() == false) and
           (friend_menu_view == nil or friend_menu_view.is_show() == false) then

            --[[local banner_info = contact_message_list_model.banner_id_to_banner_info[chat_vlist_node.current_hover_id_];
            if banner_info ~= nil then
                if banner_info.type == "group" then
                    if banner_info.group_type == "Public" then
                        chat_vlist_node.send_hide_group_tips_event(0);
                    end
                else
                    chat_vlist_node.send_hide_friend_tips_event(0);
                end
            end]]--
            chat_vlist_node.send_hide_group_tips_event(0);
            chat_vlist_node.send_hide_friend_tips_event(0);
        end
    end;

    on_left_button_up_image = function(uilib, ctrl_id)
        base_utility.log("[chat_vlist_node] on_left_button_up_image");

        local p_id = ui_utility.get_ctrl_parent_id(ctrl_id);
        local banner_info = friend_vlist_model.virtual_node_id_to_group_info[p_id];
        if banner_info ~= nil then
            if banner_info.type == "group" then
                if banner_info.group_type == "Public" then
                    local param_table = {group_id = banner_info.id};
                    event_center.send_event(group_event.SHOW_GROUP_CENTER, base_utility.table_to_json(param_table));
                end
            else
--                local table_param = {};
--                local page_info = {};
--                page_info["id"] = GAME_ID.GAEM_ID_HOME_PAGE;
--                local url_str = "http://plat.tgp.qq.com/platform/profile/icenter.html?tgp_id=" .. banner_info.id .. "&from=social";
--                page_info["url"] = url_str;
--                table_param["page_info"] = page_info;
--                comm_center.call_service("i_service_open_page", table_param);
                local data = {type = "profile", tgp_id = banner_info.id, custom_param = "&from=social"};
                web_utility.open_link_convert(base_utility.table_to_json(data));
            end
        end
    end;
    
    on_left_button_down_image = function(uilib, ctrl_id)
        base_utility.log("[chat_vlist_node] on_left_button_down_image");
        
        chat_vlist_node.stop_show_group_tip_timer();
        chat_vlist_node.send_hide_group_tips_event(1);
        chat_vlist_node.send_hide_friend_tips_event(1);
    end;

    on_right_button_down_image = function(uilib, ctrl_id, x, y)
        chat_vlist_node.stop_show_group_tip_timer();
        chat_vlist_node.send_hide_group_tips_event(1);
        chat_vlist_node.send_hide_friend_tips_event(1);
    end;

    on_right_button_up_image = function(uilib, ctrl_id, x, y)
        local rc = uilib:GetControlPtr(ctrl_id):GetRectEx(FALSE,TRUE);
        local p_ctrl = uilib:GetControlPtr(ctrl_id):GetParent();
        local p_id = p_ctrl:GetControlID();
        chat_vlist_node.on_right_button_up(uilib, p_id, rc.left + x, rc.top + y);
    end;

    set_normal_state = function(ctrl_id)
        local bk_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_hover");
        if bk_id ~= 0 then
            ui_utility.setproperty_by_id(bk_id, "Background", "");
        end;
    end;

    last_hover_node_ = nil; --本变量供下面这个函数临时用下
    set_hover_state = function(ctrl_id)
        if chat_vlist_node.last_hover_node_ then
            chat_vlist_node.set_normal_state(chat_vlist_node.last_hover_node_);
        end;
        chat_vlist_node.last_hover_node_ = ctrl_id;

        local bk_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_hover");
        ui_utility.setproperty_by_id(bk_id, "Background", "COLOR:RGB:229,229,229;");
        local g_info = friend_vlist_model.virtual_node_id_to_group_info[ctrl_id];
    end;

    last_select_node_ = nil; --本变量供下面这个函数临时用下
    set_selected_state = function(ctrl_id, b_select)
        if b_select then
            if chat_vlist_node.last_select_node_ then
                chat_vlist_node.set_selected_state(chat_vlist_node.last_select_node_, false);
            end;
            chat_vlist_node.last_select_node_ = ctrl_id;
        end;

        chat_vlist_node.restore_banner_bk();
        local bk_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_bk");
        if b_select then
            ui_utility.setproperty_by_id(bk_id, "Background", "COLOR:RGB:229,229,229;");
        else
            ui_utility.setproperty_by_id(bk_id, "Background", "");
        end;
        local g_info = friend_vlist_model.virtual_node_id_to_group_info[ctrl_id];
    end;

    send_hide_group_tips_event = function(action)
        base_utility.log(string.format("[chat_vlist_node]send_hide_group_tips_event:%d", action));
        local param = {};
        param["bShow"] = false;
        param["action"] = action;
        event_center.send_event(group_event.SHOW_GROUP_TIPS,base_utility.table_to_json(param));
    end;

    send_hide_friend_tips_event = function(action)
        local param = {};
        param["bShow"] = false;
        param["action"] = action;
        event_center.send_event(group_event.SHOW_FRIEND_TIPS, base_utility.table_to_json(param));
    end
};
